package com.aos.erp.schools.rpc.student;

import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentExam;
import com.aos.erp.schools.model.student.StudentExamHistory;
import com.aos.erp.schools.model.student.dto.StudentOneDto;
import com.aos.erp.schools.model.student.ro.exam.AddStudentExamRo;
import com.aos.erp.schools.service.student.StudentExamHistoryService;
import com.aos.erp.schools.service.student.StudentExamService;
import com.aos.erp.schools.service.student.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/1 19:22
 * @Description:    学员预约考试
 */
@Slf4j
@RestController
@RequestMapping("/service/studentExam")
@Api(tags = {"学员预约考试信息"})
public class StudentExamServiceController {

    @Resource
    private StudentExamService studentExamService;
    @Resource
    private StudentExamHistoryService studentExamHistoryService;


    @Autowired
    private StudentService studentService ;


    @PostMapping("/add")
    @ApiOperation(value = "学员预约考试信息添加")
    public boolean addStudentExam(@RequestBody List<AddStudentExamRo> ro){

//        //模拟数据
//        List<AddStudentExamRo> ro=new LinkedList<>();
//        AddStudentExamRo dd=new AddStudentExamRo();
//        dd.setName("张大大22");
//        dd.setExamSubject(1);
//        dd.setExamAddress("北京");
//        dd.setDriverId(480);
//        dd.setExamCarModel("C1");
//        dd.setExamTime(new Date());
//        dd.setExamReadyTime(new Date());
//        ro.add(dd);
        //需要对数据 进行过滤
        log.info("学员预约考试信息添加:" + ro);
        try {
            List<StudentExamHistory> listHistory=new LinkedList<>();
            for(AddStudentExamRo addro : ro)
            {
                StudentOneDto sdto=new StudentOneDto();
                sdto.setName(addro.getName());
                sdto.setDriverId(addro.getDriverId());
                Student student=studentService.selectOne(sdto);
                if(student==null)
                {
                    continue;
                }
                StudentExam se=new StudentExam();
                BeanUtils.copyProperties(addro,se);
                se.setStudentId(student.getId());
                se.setType(0);//(0 表示 122提交的    1 表示  本身平台约考记录 )
                studentExamService.saveBean(se);
                StudentExamHistory seh=new StudentExamHistory();
                BeanUtils.copyProperties(addro,seh);
                seh.setStudentId(student.getId());
                listHistory.add(seh);
            }
            studentExamHistoryService.saveBatch(listHistory);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
